Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Resolves deeply-nested object properties via dot or bracket-notation for Node.js and the browser.
The selectn npm package is a utility for safely accessing deeply nested properties in JavaScript objects. It allows you to use a string path to retrieve the value at that path, returning undefined if any part of the path does not exist.
Accessing Nested Properties
This feature allows you to access deeply nested properties within an object using a string path. If the path does not exist, it returns undefined instead of throwing an error.
const selectn = require('selectn');
const obj = { a: { b: { c: 42 } } };
const value = selectn('a.b.c', obj);
console.log(value); // 42
Handling Non-Existent Paths
This feature demonstrates how selectn handles non-existent paths gracefully by returning undefined instead of throwing an error.
const selectn = require('selectn');
const obj = { a: { b: { c: 42 } } };
const value = selectn('a.b.x', obj);
console.log(value); // undefined
Default Values
This feature shows how you can provide a default value when the path does not exist by using the logical OR operator.
const selectn = require('selectn');
const obj = { a: { b: { c: 42 } } };
const value = selectn('a.b.x', obj) || 'default';
console.log(value); // 'default'
lodash.get is a utility function from the Lodash library that provides similar functionality to selectn. It allows you to safely access deeply nested properties using a string path, with the added benefit of being part of the larger Lodash utility library.
dot-prop is another package that allows you to get, set, or delete properties in a nested object using a dot path. It offers more functionality compared to selectn, such as setting and deleting properties.
object-path is a versatile utility for accessing and manipulating deep object properties. It provides methods for getting, setting, and deleting properties, making it more feature-rich compared to selectn.
Resolves deeply-nested object properties via dot or bracket-notation for Node.js and the browser.
selectn('info.name.full', person)
person && person.info && person.info.name && person.info.name.full
if (obj && obj.a && obj.a.b && obj.a.b.c) { return obj.a.b.c; }
.group[0].section.a.seat[3]
).stats.temperature-today
).selectn
can be passed to applicative functors like Array.prototype.map and Array.prototype.filter.eval
and friends.$ component install wilmoore/selectn
$ bower install selectn
$ jam install selectn
$ volo add wilmoore/selectn
<script src="https://raw.github.com/wilmoore/selectn/master/selectn.min.js"></script>
Given the following object:
var talk = {
info: { name: 'Go Ahead, Make a Mess' }
};
Apply the selectn
function to the path
and object
parameters for error-free access to deeply nested properties.
selectn('info.name', talk);
// => 'Go Ahead, Make a Mess'
Given the following object:
var talk = {
info: { 'attendee-count': 200 }
};
Apply the selectn
function to the path
and object
parameters for error-free access to deeply nested properties.
selectn('info.attendee-count', talk);
// => 200
Given the following list:
var talks = [
{ info: { name: 'Go Ahead, Make a Mess' }},
{ info: { name: 'Silex Anatomy' }},
{ info: { name: 'Unit Testing in Python' }},
{ info: { name: 'Setting the Stage' }}
];
The generated function can be used as a predicate for map:
var query = selectn('info.name');
//=> [Function]
talks.map(query);
// => [ 'Go Ahead, Make a Mess', 'Silex Anatomy', 'Unit Testing in Python', 'Setting the Stage' ]
Given the following object of language strings:
var language = [
{ strings: { en: { name: 'english' } }},
{ strings: { es: { name: 'spanish' } }},
{ strings: { km: { name: 'khmer' } }},
{ strings: { es: { name: 'spanish' } }},
];
The generated function can be used as a predicate for filter:
var spanish = selectn('strings.es');
//=> [Function]
language.filter(spanish).length;
//=> 2
You expect the following JSON data from an XMLHttpRequest:
var data = { Client: { Message: { id: d50afb80-a6be-11e2-9e96-0800200c9a66 } } };
Access the Client.Message.id
property and log the result to the console (using promises):
$.ajax({...})
.then(selectn('Client.Message.id'))
.then(console.log.bind(console));
//=> d50afb80-a6be-11e2-9e96-0800200c9a66
NOTE: Even if you don't use this methodology in production code, it can be a handy timesaver in terms of quick debugging.
In larger, data-driven applications, there tends to be a need to do a lot of deep object access which can quickly lead to code like this:
var name;
if (contact && contact.info && contact.info.name) {
name = contact.info.name.full || 'unknown';
}
The following is much more concise:
var name = selectn('info.name.full')(contact) || 'unknown';
In case you care about this sort of thing, we are able to do normal function application as well as partially apply when that is convenient due to currying
.
selectn('info.name.full', contact)
(normal function application)selectn('info.name.full')(contact)
(partial application without a partial
helper like Function.prototype.bind
)Since selectn
is a 2-ary function, we don't need to use an external library for currying as the algorithm is simple.
eval
in disguise).MIT
FAQs
Curried property accessor function that resolves deeply-nested object properties via dot/bracket-notation string path while mitigating TypeErrors via friendly and composable API.
We found that selectn demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.